package com.jeecg.modules.jmreport.controller;

import com.jeecg.modules.jmreport.test.AuthVerificationService;
import com.jeecg.modules.jmreport.test.IpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
//@RequestMapping("/api/auth")
public class AuthController {

    @Resource
    private AuthVerificationService authService;

    /**
     * 用户验证接口
     */
    @GetMapping("/api/auth/verify")
    @ResponseBody
    public Map<String, Object> verifyUser(HttpServletRequest request,
                                          @RequestParam("companyId") String companyId,
                                          @RequestParam("authCode") String authCode,
                                          @RequestParam(value = "remoteUserIP",required = false,defaultValue = "") String remoteUserIP) {
        Map<String, Object> result = new HashMap<>();

        try {
            boolean isVerified = authService.verifyUser(request,companyId, authCode,remoteUserIP);


            result.put("success", isVerified);
            result.put("ip", IpUtils.getRealClientIp(request));

            if (isVerified) {
                result.put("message", "验证成功");
            } else {
                result.put("message", "验证失败，IP或授信码不正确");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "验证过程发生错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 用户验证接口
     */
    @GetMapping("/api/auth/verifyapp")
    @ResponseBody
    public Map<String, Object> verifyAppUser(HttpServletRequest request,
                                          @RequestParam("companyId") String companyId,
                                          @RequestParam("authCode") String authCode) {
        Map<String, Object> result = new HashMap<>();

        try {
            boolean isVerified = authService.verifyAppUser(request,companyId, authCode);


            result.put("success", isVerified);
            result.put("ip", IpUtils.getRealClientIp(request));

            if (isVerified) {
                result.put("message", "验证成功");
            } else {
                result.put("message", "验证失败，IP或授信码不正确");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "验证过程发生错误: " + e.getMessage());
        }

        return result;
    }
}